package com.jt.serviceImpl;

import com.jt.pojo.Content;
import com.jt.service.ContentService;
import com.jt.util.HttpClientService;
import com.jt.utils.ObjectMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 使用httpClient实现首页广告展示
 * @author dc
 * @Classname ContenServiceImpl
 * @Date 2020/3/25 11:25
 */
@Service
@PropertySource("classpath:/properties/Content.properties")
public class ContentServiceImpl implements ContentService {

    @Value("${Content.service.url}")
    private String contentServiceUrl;
    @Value("${Content.BigPoster.url}")
    private String contentCatIdUrl;
    @Value("${Content.News.url}")
    private String contentNewUrl;


    @Autowired
    private HttpClientService httpClientService;


    @Override
    public String getContentList() {
        String json = httpClientService.doGet(contentServiceUrl+contentCatIdUrl);
        List<Content> list = ObjectMapperUtil.toList(json, Content.class);
        List<Map> resultList=new ArrayList<>();
        for (Content content:list) {
            Map map = new HashMap();
            map.put("src",content.getPic());
            map.put("height",240);
            map.put("width",670);
            map.put("srcB",content.getPic2());
            map.put("heightB",240);
            map.put("widthB",550);
            map.put("href",content.getUrl());
            map.put("alt",content.getTitle());
            resultList.add(map);
        }
        System.out.println();
        return ObjectMapperUtil.toJson(resultList);
    }

    @Override
    public List<Content> getNews() {
        String json = httpClientService.doGet(contentServiceUrl+contentNewUrl);
        return ObjectMapperUtil.toList(json, Content.class);
    }


}
